package com.cqeec.view;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.cqeec.domain.CallRoll;
import com.cqeec.domain.Student;
import com.cqeec.service.StudentService;
import com.cqeec.service.imp.StudentServiceImpl;
import com.cqeec.utils.JDBCUtils;

import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;

/**
 * 学生查看考勤界面
 * 
 * @author Administrator
 *
 */
public class StudentAttend extends JInternalFrame {
	private JTable table;
	private JTextField textField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * 
	 * @param myStudent
	 */
	public StudentAttend(Student myStudent) {
		setIconifiable(true);
		setClosable(true);
		setTitle("查看考勤");
		setBounds(100, 100, 686, 451);

		JScrollPane attendance = new JScrollPane();

		JLabel label = new JLabel("学生学号：");

		textField = new JTextField();
		textField.setColumns(10);
		System.out.println(myStudent.toString());
		textField.setText(myStudent.getStudentNum());
		JButton button = new JButton("查询");
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup()
				.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(Alignment.TRAILING,
								groupLayout.createSequentialGroup().addContainerGap().addComponent(attendance,
										GroupLayout.DEFAULT_SIZE, 642, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup().addGap(39).addComponent(label)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(textField, GroupLayout.PREFERRED_SIZE, 195, GroupLayout.PREFERRED_SIZE)
								.addGap(18).addComponent(button)))
				.addContainerGap()));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(Alignment.TRAILING,
				groupLayout.createSequentialGroup().addGap(27)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(label)
								.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(button))
						.addPreferredGap(ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
						.addComponent(attendance, GroupLayout.PREFERRED_SIZE, 230, GroupLayout.PREFERRED_SIZE)
						.addGap(90)));

		table = new JTable();

		table.setModel(new DefaultTableModel(new Object[][] {},
				new String[] { "\u5B66\u751F", "\u8BFE\u7A0B\u540D", "\u65F7\u8BFE\u6B21\u6570" }));
		attendance.setViewportView(table);
		getContentPane().setLayout(groupLayout);
		seeTable();
		

	}

	/**
	 * 表格初始化
	 */
	public void seeTable() {

		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		dtm.setRowCount(0);
		Connection conn = null;
		conn = JDBCUtils.getConnection();
		StudentService service = new StudentServiceImpl();
		ResultSet rs = service.getallCallRoll(conn);
		try {
			while(rs.next()) {
				Vector<Object> v=new Vector<>();
				v.add(rs.getString("studentName"));
				v.add(rs.getString("courseName"));
				v.add(rs.getString("courseAbsent"));
				dtm.addRow(v);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

	}
}
